package com.zgp.config;


import com.zgp.constants.AiPromptConstants;
import com.zgp.tools.MobileDataMealTool;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * springai 配置类
 * @author zgp
 * @version V1.0
 * @date 2025-04-20 19:42
 */
@Configuration
@RequiredArgsConstructor
public class SpringAiConfig {
    private final MobileDataMealTool mobileDataMealTool;

    /**
     * 声明SpringAI 对话客户端 Bean
     */
    @Bean
    public ChatClient chatClient(ChatClient.Builder chatClientBuilder
    , Advisor messageChatMemoryAdvisor, Advisor simpleLoggerAdvisor) {
        return chatClientBuilder
                .defaultSystem(AiPromptConstants.CHAT_SYSTEM_PROMPT)
                .defaultTools(mobileDataMealTool)
                .defaultAdvisors(messageChatMemoryAdvisor, simpleLoggerAdvisor)
                .build();
    }

    /**
     * 声明【日志增强】Advisor Bean
     *
     * @return
     */
    @Bean
    public Advisor simpleLoggerAdvisor() {
        return new SimpleLoggerAdvisor();
    }

    /**
     * 声明 基于内存存储会话 对象
     * @return
     */
    @Bean
    public ChatMemory chatMemory(){
        return new InMemoryChatMemory();
    }

    /**
     * 声明 会话存储 Advisor
     * @param chatMemory 会话存储类型
     * @return
     */
    @Bean
    public Advisor messageChatMemoryAdvisor(ChatMemory chatMemory){
        return new MessageChatMemoryAdvisor(chatMemory);
    }

    /**
     * 声明 基于内存的向量库
     * @param embeddingModel 向量模型
     * @return
     */
    @Bean
    public VectorStore vectorStore(EmbeddingModel embeddingModel){
        return SimpleVectorStore.builder(embeddingModel).build();
    }
}
